
/**
 * @Author: 陈瑞鹏
 * @Date:   2022-12-08 14:48:07
 * @Last Modified by:   陈瑞鹏
 * @Last Modified time: 2022-12-15 10:05:49
 */
import treasureCtr from "../ctrs/treasure/treasureCtr";
import treasureEntity from "../ctrs/treasure/treasureEntity";
import BaseGameComponent from "../libs/core/BaseGameComponent";
import { PureMVCEvents } from "../libs/core/PureMVCEvents";
import { getTimehms } from "../libs/core/utils";
import { INotification } from "../libs/pureMvc/Interfaces";
import bxDescCom from "./bxDescCom";

const { ccclass, property } = cc._decorator;
@ccclass
export default class bxCom extends BaseGameComponent {

  @property({ type: cc.Node, tooltip: "描述" })
  descNode: cc.Node = null;

  @property({ type: cc.Node, tooltip: "宝箱1" })
  item1: cc.Node = null;

  @property({ type: cc.Node, tooltip: "宝箱2" })
  item2: cc.Node = null;

  @property({ type: cc.Node, tooltip: "宝箱3" })
  item3: cc.Node = null;

  @property({ type: cc.Node, tooltip: "宝箱4" })
  item4: cc.Node = null;

  currentKey: any = null;

  public listNotificationInterests(): string[] {
    return [PureMVCEvents.ON_TREASURE_UDPATE];
  }

  public handleNotification(notification: INotification): void {
    let evtName = notification.getName();
    switch (evtName) {
      case PureMVCEvents.ON_TREASURE_UDPATE:
        this.drawTreasure();
        break;
    }
  }

  drawTreasure() {
    let list: treasureEntity[] = treasureCtr.ins.treasureEnyDic.getValueList();
    for (let i = 0; i < 4; i++) {
      let item = this.node.getChildByName("item" + i);
      let txt = item.getChildByName("txt");
      let ele: treasureEntity = list[i];
      if (ele && ele.step <= 3) {
        item.active = true;
        cc.resources.load(ele.treasureT.iconUrl, cc.SpriteFrame, (err, sp: cc.SpriteFrame) => {
          item.getComponent(cc.Sprite).spriteFrame = sp;
        })
        if (ele.step == 1) {
          txt.active = false;
        } else {
          txt.active = true;
          if (ele.step == 2) {
            let now = new Date().getTime();
            txt.getComponent(cc.Label).string = getTimehms(ele.openTime - Math.ceil(now / 1000));
            // console.log(ele.openTime, now, (now > ele.openTime));
            if (Math.ceil(now / 1000) > ele.openTime) {
              txt.getComponent(cc.Label).string = "可领取";
            }
          } else if (ele.step == 3) {
            txt.getComponent(cc.Label).string = "可领取";
          }
        }
      } else {
        item.active = false;
      }
    }
  }


  onClick(e, key) {

    if (key == this.currentKey) return;
    console.log(key);

    this.descNode.active = true;
    this.descNode.getComponent(bxDescCom).drawUi(key);
  }

  /**
   * 关闭描述界面
   */
  onCloseDesc() {
    this.descNode.active = false;
  }

  private _interval: number = 0;

  protected update(dt: number): void {
    this._interval++;
    if (this._interval > 60) {
      this.drawTreasure();
      this._interval = 0;
    }
  }
}